package fi.profecons.csswindows;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import fi.profecons.csswindows.toolkit.Frame;
import fi.profecons.csswindows.toolkit.Input;
import fi.profecons.csswindows.toolkit.SelectBox;

class XmlHandler extends DefaultHandler {

	Element element = null;

	String currentElement = new String();

	State state = null;

	Frame window = null;

	String type = null;

	String action = null;

	WindowApplication w = null;

	StringBuffer sb = new StringBuffer();

	Element e = null;

	Element source = null;

	private FileHandler fileHandler;

	public XmlHandler(State state, WindowApplication w) {
		super();
		this.state = state;
		this.w = w;
		this.fileHandler = new FileHandler();
	}

	public void startDocument() {
		System.out.println("Start document");
	}

	public void endDocument() {
		System.out.println("End document");
	}

	public void startElement(String uri, String name, String qName,
			Attributes atts) {
		sb = new StringBuffer();
		if (name.equals("state")) {
			type = atts.getValue("type");
		}
		if (type.equals("initialize")) {
			state.init();
			state.setApp(w);
		}
		if (type.equals("uploadComplete")) {
			fileHandler.fileUploadComplete(state);
		}
		if (type.equals("update")) {
			state.scriptingOff();
		} else {
			state.scriptingOn();
		}
	}

	public void characters(char ch[], int start, int length) {
		for (int i = start; i < start + length; i++) {
			sb.append(ch[i]);
		}
	}

	public void endElement(String uri, String name, String qName) {
		System.out.println(name + ": " + sb.toString());
		if (name.equals("id")) {
			element = state.get(sb.toString());
		}
		if (name.equals("x") && element != null) {
			element.setX(Integer.parseInt(sb.toString()));
		}
		if (name.equals("y") && element != null) {
			element.setY(Integer.parseInt(sb.toString()));
		}
		if (name.equals("width") && element != null) {
			element.setWidth(Integer.parseInt(sb.toString()));
		}
		if (name.equals("height") && element != null) {
			element.setHeight(Integer.parseInt(sb.toString()));
		}

		if (element instanceof Input) {
			Input input = (Input) element;
			if (name.equals("value") && element != null) {
				input.setValue(sb.toString());
			}
		}
		if (element instanceof SelectBox) {
			SelectBox selectBox = (SelectBox) element;
			if (name.equals("selected") && element != null) {
				selectBox.select(Integer.parseInt(sb.toString()));
			}
		}
		if (element instanceof Frame) {
			Frame frame = (Frame) element;
			if (name.equals("canvaswidth") && element != null) {
				frame.setCanvasWidth(Integer.parseInt(sb.toString()));
			}
			if (name.equals("canvasheight") && element != null) {
				frame.setCanvasHeight(Integer.parseInt(sb.toString()));
			}
		}

		if (type.equals("action")) {
			if (name.equals("elementid")) {
				e = state.get(sb.toString());
			}
			if (name.equals("source")) {
				source = state.get(sb.toString());
			}
			if (name.equals("action") && e != null) {
				action = sb.toString();
			}
		}
		if (name.equals("state")) {
			if (type.equals("action") && e != null) {
				ActionListener actionListener = e.getActionListener();
				if (actionListener != null) {
					if (source != null) {
						actionListener.handleAction(e, source, action);
					} else {
						actionListener.handleAction(e, action);
					}
				}
				source = null;
			}
		}
	}

}
